package com.edu.tool.util.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SimpleDateUtil {
    public static Date TimeStamp2Date(String timestampString ){
        Long timestamp = Long.parseLong(timestampString)*1000;
         return new Date(timestamp);
    }
    /**
     * @PATTERN_YYYYMMDD:日期模式yyyyMMdd
     */
    public static final String PATTERN_YYYY_MM = "yyyy-MM";

    /**
     * @PATTERN_YYYYMMDD:日期模式yyyyMMdd
     */
    public static final String PATTERN_YYYYMM = "yyyyMM";

    /**
     * @PATTERN_YYYYMMDD:日期模式yyyyMMdd
     */
    public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * @PATTERN_YYYYMMDD:日期模式yyyyMMdd HH
     */
    public static final String PATTERN_YYYY_MM_DD_HH = "yyyy-MM-dd HH";

    /**
     * @PATTERN_YYYYMMDD:日期模式yyyyMMdd
     */
    public static final String PATTERN_YYYYMMDD = "yyyyMMdd";

    /**
     * @PATTERN_YYYYMMDD:时分秒
     */
    public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    /**
     * @PATTERN_YYYYMMDD:时分秒
     */
    public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * @PATTERN_YYYYMMDD:毫秒
     */
    public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";

    /**
     * @param date
     * @param month
     * @return
     * @Author：thy
     * @CreateTime：下午3:37:05
     * @Description：计算投资到期日
     */
    public static Date getInvestEndDate(Date date, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DAY_OF_MONTH);
        c.add(Calendar.MONTH, month);
        int daynew = c.get(Calendar.DAY_OF_MONTH);
        if (day != daynew) {//若不同，向后偏移一天
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        //计算上一日为到期日
        c.add(Calendar.DAY_OF_MONTH, -1);
        return c.getTime();
    }

    /**
     * @param date 待加工日期
     * @return
     * @paramdays 变动天数
     * @Author：thy
     * @CreateTime：下午5:45:39
     * @Description：给某个日期加上某个天数得到新的日期
     */
    public static Date addMonthForDate(Date date, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }

    public static Date addHoursForDate(Date date, int hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, hours);
        return c.getTime();
    }

    public static Date addWeekForDate(Date date, int week) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.WEEK_OF_MONTH, week);
        return c.getTime();
    }


    public static Date addForDate(Date date, int fild, int plus) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(fild, plus);
        return c.getTime();
    }

    /**
     * @param date 待加工日期
     * @param days 变动天数
     * @return
     * @Author：thy
     * @CreateTime：下午5:45:39
     * @Description：给某个日期加上某个天数得到新的日期
     */
    public static Date addDaysForDate(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTime();
    }

    /**
     * @param date 待加工日期
     * @param min  变动分钟
     * @return
     * @Author：thy
     * @CreateTime：下午5:45:17
     * @Description：给某个日期加上某个分钟得到新的日期
     */
    public static Date addMinsForDate(Date date, int min) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, min);
        return c.getTime();
    }

    /**
     * @param date
     * @param pattern
     * @return
     * @Author：thy
     * @CreateTime：下午5:45:09
     * @Description：
     */
    public static String toString(Date date, String pattern) {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }


    /**
     * @param date
     * @param patternFrom
     * @param patternTo
     * @return
     * @Author：thy
     * @CreateTime：下午3:45:09
     * @Description：转化日期对象格式
     */
    public static String changeFormat(String date, String patternFrom,
                                      String patternTo) {
        DateFormat from = new SimpleDateFormat(patternFrom);
        DateFormat to = new SimpleDateFormat(patternTo);
        try {
            return to.format(from.parse(date));
        } catch (ParseException e) {
            return date;
        }
    }


    /**
     * @param input
     * @param format
     * @return
     * @Author：thy
     * @CreateTime：下午12:04:06
     * @Description：判断format是否匹配
     */
    public static boolean isMatch(String input, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            if (!input.equals(df.format(df.parse(input))))
                return false;
        } catch (Throwable e) {
            return false;
        }
        return true;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return
     * @Author：thy
     * @CreateTime：下午3:45:09
     * @Description：计算日期相差天数
     */
    public static int dateDiff(String beginDate, String endDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        try {
            long to = df.parse(endDate).getTime();
            long from = df.parse(beginDate).getTime();
            return (int) ((to - from) / (1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @param beginDate
     * @param endDate
     * @return
     * @Author：thy
     * @CreateTime：下午4:04:02
     * @Description：计算日期相差分钟
     */
    public static int dateDiffMinutes(Date beginDate, Date endDate) {
        long to = endDate.getTime();
        long from = beginDate.getTime();
        return (int) ((to - from) / (1000 * 60));
    }

    /**
     * @param dateStr 日期字符串
     * @param partten 日期格式串
     * @return
     * @Author：thy
     * @CreateTime：下午3:45:09
     * @Description：计算日期相差天数
     */
    public static Date parseStr(String dateStr, String partten) {
        DateFormat df = new SimpleDateFormat(partten);
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @param d1 日期1
     * @param d2 日期2
     * @return
     * @parampartten日期格式
     * @Author：thy
     * @CreateTime：下午3:45:09
     * @Description：按照日期格式比较日期是否相等
     */
    public static boolean compareDate(Date d1, Date d2, String partten) {
        DateFormat df = new SimpleDateFormat(partten);
        String d1Str = df.format(d1);
        String d2Str = df.format(d2);
        if (d1Str.equals(d2Str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param d1 日期1
     * @param d2 日期2
     * @return
     * @Author：thy
     * @CreateTime：下午3:45:09
     * @Description：日期相减
     */
    public static long subtractDate(Date d1, Date d2) {
        return d1.getTime() - d2.getTime();
    }

    /**
     * @return
     * @Author：thy
     * @CreateTime：上午9:09:02
     * @Description：
     */
    public static String yyyyMMdd() {
        return SimpleDateUtil.toString(new Date(), SimpleDateUtil.PATTERN_YYYYMMDD);
    }

    /**
     * @return
     * @Author：thy
     * @CreateTime：上午9:09:48
     * @Description：
     */
    public static String yyyyMMddhhmmss() {
        return SimpleDateUtil.toString(new Date(), SimpleDateUtil.PATTERN_yyyyMMddHHmmss);
    }

    public static void main(String[] args) {
        System.out.println(SimpleDateUtil.dateDiffMinutes(SimpleDateUtil.parseStr("20160930160000", SimpleDateUtil.PATTERN_yyyyMMddHHmmss), new Date()));
    }

    public static String getMondayOfWeek(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int offset = 0;
        if (dayOfWeek == 1)
            offset = -6;
        else
            offset = 2 - dayOfWeek;
        c.add(Calendar.DAY_OF_MONTH, offset);
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYYMMDD);
    }

    public static String getSundayOfWeek(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int offset = 0;
        if (dayOfWeek == 1)
            offset = 0;
        else
            offset = 8 - dayOfWeek;
        c.add(Calendar.DAY_OF_MONTH, offset);
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYYMMDD);
    }

    public static String getFirstDayOfMonth(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-01");
        return format.format(date);
    }

    public static String getLastDayOfMonth(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int maxDay = c.getActualMaximum(c.DAY_OF_MONTH);
        String pattern;
        if (maxDay < 10) {
            pattern = "yyyy-MM-0" + maxDay;
        } else {
            pattern = "yyyy-MM-" + maxDay;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static String getFirstDayOfYear(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-01-01");
        return format.format(date);
    }

    public static String getLastDayOfYear(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYYMMDD);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-12-31");
        return format.format(date);
    }

    public static List<String> getTimeSplitList(String yyyyMMdd, String timeType) {
        List<String> result = new ArrayList<String>();
        if ("day".equals(timeType)) {
            for (int i = 0; i < 24; i++) {
                result.add(i < 10 ? "0" + i : "" + i);
            }
        } else if ("week".equals(timeType)) {
            String monday = getMondayOfWeek(yyyyMMdd);
            Date date = SimpleDateUtil.parseStr(monday, SimpleDateUtil.PATTERN_YYYYMMDD);
            result.add(monday);
            for (int i = 1; i <= 6; i++) {
                result.add(SimpleDateUtil.toString(SimpleDateUtil.addDaysForDate(date, i), SimpleDateUtil.PATTERN_YYYYMMDD));
            }
        } else if ("month".equals(timeType)) {
            Date curDate = SimpleDateUtil.parseStr(yyyyMMdd.substring(0, 6) + "01", SimpleDateUtil.PATTERN_YYYYMMDD);
            Date firstDayOfNextMonth = SimpleDateUtil.addMonthForDate(curDate, 1);

            while (curDate.before(firstDayOfNextMonth)) {
                result.add(SimpleDateUtil.toString(curDate, SimpleDateUtil.PATTERN_YYYYMMDD));
                curDate = SimpleDateUtil.addDaysForDate(curDate, 1);
            }
        } else if ("year".equals(timeType)) {
            Date curDate = SimpleDateUtil.parseStr(yyyyMMdd.substring(0, 4) + "0101", SimpleDateUtil.PATTERN_YYYYMMDD);
            for (int i = 1; i <= 12; i++) {
                result.add(SimpleDateUtil.toString(curDate, "yyyyMM"));
                curDate = SimpleDateUtil.addMonthForDate(curDate, 1);
            }
        }

        return result;
    }

    public static int daysBetween2Date(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;

        try {
            cal.setTime(sdf.parse(startTime));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(endTime));
            time2 = cal.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        long between_days = (time2 - time1) / (1000 * 3600);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static Date stampToDate(String timeStamp, String pattern){
        Long time = new Long(timeStamp);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String d = simpleDateFormat.format(time);
        Date date = null;
        try {
            date = simpleDateFormat.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static List<String> getBetweenDates(String startTime, String endTime) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat(SimpleDateUtil.PATTERN_YYYYMMDD);
        Date beforeDate = SimpleDateUtil.parseStr(startTime, SimpleDateUtil.PATTERN_YYYYMMDD);
        Date afterDate = SimpleDateUtil.parseStr(endTime, SimpleDateUtil.PATTERN_YYYYMMDD);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(beforeDate);
        while(beforeDate.getTime()<=afterDate.getTime()){
            result.add(sdf.format(beforeDate.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            beforeDate = tempStart.getTime();
        }
        return result;
    }
    // 获取当前时间的年
    public static Integer getYearByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }
    // 获取当前时间的月
    public static Integer getMonthByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }
    // 获取当前时间的日
    public static Integer getDayByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_MONTH);
    }
    // 获取当前时间的小时数
    public static Integer getHourByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.HOUR_OF_DAY);
    }

    // 获取当前时间的分钟数
    public static Integer getMinuteByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MINUTE);
    }

    // 获取当前时间的秒数
    public static Integer getSecondByCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.SECOND);
    }

    // 获取当月的第一天
    public static String getFirstDayFromMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(SimpleDateUtil.PATTERN_YYYY_MM_DD);
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(cale.getTime());
    }
    // 获取当月的最后一天
    public static String getLastDayFromMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(SimpleDateUtil.PATTERN_YYYY_MM_DD);
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cale.getTime());
    }

    // 获取指定日期的后一天
    public static String getAfterDateByDate (String date) {
        Calendar c = Calendar.getInstance();
        Date d = null;
        try {
            d = new SimpleDateFormat(SimpleDateUtil.PATTERN_YYYY_MM_DD).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(d);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day+1);
        String dayAfter = new SimpleDateFormat(SimpleDateUtil.PATTERN_YYYY_MM_DD).format(c.getTime());
        return dayAfter;
    }
    // 日期所在星期的第一天
    public static String getFirstDayOfWeek(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int offset = 0;
        if (dayOfWeek == 1)
            offset = -6;
        else
            offset = 2 - dayOfWeek;
        c.add(Calendar.DAY_OF_MONTH, offset);
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYY_MM_DD);
    }
    // 日期所在星期的最后一天
    public static String getLastDayOfWeek(String yyyyMMdd) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int offset = 0;
        if (dayOfWeek == 1)
            offset = 0;
        else
            offset = 8 - dayOfWeek;
        c.add(Calendar.DAY_OF_MONTH, offset);
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYY_MM_DD);
    }
    // 根据日期定义(年月日)获取该日期之前满足条件的日期
    public static String getBeforeDateOfDate(String yyyyMMdd, Integer num, String ymd) {
        Date date;
        if (yyyyMMdd.contains(" ")) {
            date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        } else {
            date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYY_MM_DD);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (ymd.equals("year")) {
            c.set(Calendar.YEAR, c.get(Calendar.YEAR) - num);
        } else if (ymd.equals("month")) {
            int day = c.get(Calendar.DAY_OF_MONTH);
            int tempDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            int year = c.get(Calendar.YEAR);
            // 得到2月的最后一天
            int tempLastDay = getLastDayOfMonthAndYear(year, 2);
            // 如果是月末
            if (day == tempDay || day > tempLastDay) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num);
            }
        } else if (ymd.equals("day")) {
            c.set(Calendar.DATE, c.get(Calendar.DATE) - num);
        } else if (ymd.equals("hour")) {
            c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) - num);
            return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        } else if (ymd.equals("slmonth")) {
            c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num);
            c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        } else if (ymd.equals("elmonth")) {
            c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num);
            c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        } else if (ymd.equals("lamonth")) {
            c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num + 1);
        } else if (ymd.equals("fdmonth")) {
            if (num != null) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num);
            }
            c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        } else if (ymd.equals("ldmonth")) {
            if (num != null) {
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int lastDay = getLastDayOfMonthAndYear(year, month);
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
            }
        } else if (ymd.equals("ddmonth")) {
            int day = c.get(Calendar.DAY_OF_MONTH);
            int tempDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            // 得到2月的最后一天
            int tempLastDay = getLastDayOfMonthAndYear(year, month);
            // 如果是月末
            if (day == tempDay || day > tempLastDay) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) - num);
            }
        }
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYY_MM_DD);
    }
    // 根据日期定义(年月日)获取该日期之后满足条件的日期
    public static String getAfterDateOfDate(String yyyyMMdd, Integer num, String ymd) {
        Date date;
        if (yyyyMMdd.contains(" ")) {
            date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        } else {
            date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_YYYY_MM_DD);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (ymd.equals("year")) {
            c.set(Calendar.YEAR, c.get(Calendar.YEAR) + num);
        } else if (ymd.equals("month")) {
            int day = c.get(Calendar.DAY_OF_MONTH);
            int tempDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            int year = c.get(Calendar.YEAR);
            // 得到2月的最后一天
            int tempLastDay = getLastDayOfMonthAndYear(year, 2);
            // 如果是月末
            if (day == tempDay || day > tempLastDay) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num);
            }
        } else if (ymd.equals("day")) {
            c.set(Calendar.DATE, c.get(Calendar.DATE) + num);
        } else if (ymd.equals("hour")) {
            c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + num);
            return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        } else if (ymd.equals("lamonth")) {
            c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num);
        } else if (ymd.equals("fdmonth")) {
            if (num != null) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num);
            }
            c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        } else if (ymd.equals("ldmonth")) {
            if (num != null) {
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int lastDay = getLastDayOfMonthAndYear(year, month);
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
            }
        } else if (ymd.equals("ddmonth")) {
            int day = c.get(Calendar.DAY_OF_MONTH);
            int tempDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            // 得到月的最后一天
            int tempLastDay = getLastDayOfMonthAndYear(year, month);
            // 如果是月末
            if (day == tempDay || day > tempLastDay) {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num + 1);
                c.set(Calendar.DAY_OF_MONTH, 0);
            } else {
                c.set(Calendar.MONTH, c.get(Calendar.MONTH) + num);
            }
        }
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_YYYY_MM_DD);
    }

    // 获取日期后一秒
    public static String getAfterSecondOfDate(String yyyyMMdd, Integer num) {
        Date date = SimpleDateUtil.parseStr(yyyyMMdd, SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.SECOND, c.get(Calendar.SECOND) + num);
        return SimpleDateUtil.toString(c.getTime(), SimpleDateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
    }

    public static String getFirstDayofMonth(String partten, Integer year, Integer month){
        DateFormat df = new SimpleDateFormat(partten);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return df.format(c.getTime());
    }

    public static String getFirstMinofDay(String partten, Integer year, Integer month, Integer day){
        DateFormat df = new SimpleDateFormat(partten);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return df.format(c.getTime());
    }

    public static int getLastDayOfMonthAndYear(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        int minday = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, minday);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        return lastDay;
    }
}
